<template>
  <div class="container">
    <div class="app-container">
      <div class="title">
        <el-row>
          <el-button type="primary" @click="isShow=true">新增模板</el-button>
          <!-- 弹框 -->
          <AddDept ref="addPrem" :obj="obj" :show-dialog.sync="isShow" @premSuccess="getCarriagesList" />
        </el-row>
      </div>
      <el-row>
        <el-table
          :data="tableData"
          style="width: 100% "
        >
          <el-table-column
            prop="templateType"
            label="模板类型"
          >
            <template v-slot="{row}">
              <span v-if="row.templateType===1">同城寄</span>
              <span v-else-if="row.templateType===2">省内寄</span>
              <span v-else-if="row.templateType===3">经济区互寄</span>
              <span v-else>跨省</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="transportType"
            label="运送类型"
          >
            <template v-slot="{row}">
              <span v-if="row.transportType===1">普快</span>
              <span v-else>特快</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="associatedCityList"
            label="关联城市"
          >
            <template v-slot="{row}">
              <span v-for="(item,index) in row.associatedCityList" :key="index">{{ city[item] }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="firstWeight"
            label="首重"
            width="120"
          />
          <el-table-column
            prop="continuousWeight"
            label="续重"
            width="300"
          />
          <el-table-column
            prop="lightThrowingCoefficient"
            label="轻抛系数"
            width="120"
          />
          <el-table-column
            align="center"
            fixed="right"
            label="操作"
            width="100"
          >
            <template v-slot="{row}">
              <el-button type="text" size="small" style="color:#419eff" @click="editFn(row)">编辑</el-button>
              <span style="color:#dcdfe6;padding: 0 10px;">|</span>

              <el-button slot="reference" size="small" type="text" @click="delFn(row.id)">删除</el-button>

            </template>
          </el-table-column>
        </el-table>
      </el-row>
    </div>
  </div>
</template>

<script>
import AddDept from './components/add-dept'
import { getCarriagesListApi, delCarriagApi } from '@/api/transit'
export default {
  components: { AddDept },
  data() {
    return {

      tableData: [],
      obj: {},
      isShow: false,
      city: {
        1: '全国',
        2: '京津冀',
        3: '江浙沪',
        4: '川渝',
        5: '黑吉辽'
      }

    }
  },
  created() {
    this.getCarriagesList()
  },
  methods: {
    async getCarriagesList() {
      const res = await getCarriagesListApi()
      // console.log(res)
      this.tableData = res
    },
    editFn(row) {
      this.obj = row
      this.$nextTick(() => {
        this.$refs.addPrem.editData()
      })
      this.isShow = true
    },
    delFn(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 发送请求
        await delCarriagApi(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        // 重新渲染界面
        this.getCarriagesList()
      }).catch()
    }
  }}
</script>

<style lang="scss" scoped>
::v-deep .el-table th.el-table__cell.is-leaf{
  background-color: #f8faff;
}

::v-deep .el-dialog__header{
  background-color: #f3f4f7;
}
.container{
  padding: 20px;
  .app-container{
    padding: 28px;
  background-color: #fff;
  .el-table{
    border: 1px solid #ebeef5;
  }
    .title{
      margin-bottom: 20px;
    }
  }
}
</style>

